#include<allegro.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
struct mario{
	int x;
	int y;
	BITMAP *img_right[5];
	BITMAP *img_left[5];
};

typedef struct mario mario;

void attendre(float temps);

int main(){
    FILE *f=NULL;
	int i,j=0,p,k;
	BITMAP *buffer,*ing; //ing:variable bitmap sur la sequence des scenes ;; buffer:memoire tampon 
	BITMAP *image;   //image:variable bitmap sur les images de jimmy
	BITMAP *background[5];//background:tableau de bitmap sur la scene 
	mario MAR;
	allegro_init();
	
	if(install_keyboard() ==-1){
		allegro_message("ERROR! %s",allegro_error);
		return -1;
	}
	 install_mouse();
	
	set_color_depth(16);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1280,750,0,0) !=0){
		set_gfx_mode(GFX_TEXT,0,0,0,0);
		allegro_message("unable to intialize video mode!\n%s\n",allegro_error);
	return 1;}
	
	
	buffer = create_bitmap(1280,750);
	background[0]=load_bitmap("./scr/0.bmp",NULL);
	background[1]=load_bitmap("./scr/1.bmp",NULL);
	background[2]=load_bitmap("./scr/2.bmp",NULL);
	background[3]=load_bitmap("./scr/3.bmp",NULL);
	background[4]=load_bitmap("./scr/4.bmp",NULL);
	MAR.img_right[0]=load_bitmap("./images/l/r0.bmp",NULL);
	MAR.img_right[1]=load_bitmap("./images/l/r1.bmp",NULL);
	MAR.img_right[2]=load_bitmap("./images/l/r2.bmp",NULL);
	MAR.img_right[3]=load_bitmap("./images/l/r3.bmp",NULL);
	MAR.img_right[4]=load_bitmap("./images/l/r4.bmp",NULL);
	MAR.img_left[0]=load_bitmap("./images/l/l0.bmp",NULL);
	MAR.img_left[1]=load_bitmap("./images/l/l1.bmp",NULL);
	MAR.img_left[2]=load_bitmap("./images/l/l2.bmp",NULL);
	MAR.img_left[3]=load_bitmap("./images/l/l3.bmp",NULL);
	MAR.img_left[4]=load_bitmap("./images/l/l4.bmp",NULL);
	
	

	
	 
	
	
	for(i=0;i<5;i++){
	
		if (MAR.img_right[i]==NULL && background[i]==NULL && MAR.img_left[i]==NULL)
		{allegro_message("could not load images");
	 	allegro_exit();}}
	 	
	MAR.x=1;    //initialisation de l'x de jimmy
	MAR.y=324;  //initialisation de l'y de jimmy
	
	blit(background[0],buffer,0,0,0,0,SCREEN_W,SCREEN_H);
	draw_sprite(buffer,MAR.img_right[0],MAR.x,MAR.y);
	blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
	i=0;
	k=MAR.y;
	p=MAR.y-200;
	image=MAR.img_right[0];
		
		ing=background[0];
	while (!key[KEY_ESC])
	{
	    
	show_mouse(screen);



	 if ( mouse_b & 1 ){//si en clique sur bouton droit de souris
	 	 f=fopen("log.txt","a");
	 fprintf(f,"%d  %d\n",mouse_x,mouse_y);// ecrit en fin sur le fichier log.txt les cordonnés de curseur avec mouse_x,mouse_y predefni avec allegro en install_mouse
	 	fclose(f);
	 }

		
		
        if(MAR.x>120 && MAR.x<250 ){ing=background[1];}
		if(MAR.x>410 && MAR.x<536 ){ing=background[2];}
		if(MAR.x>689 && MAR.x<720 ){ing=background[3];}
        if(MAR.x>1000 && MAR.x<1100){ing=background[4];}
        if(MAR.x>1200 ){return 0;} 


if (key[KEY_RIGHT] && MAR.x < SCREEN_W -50)
		{i++;
		if(i>4){i=0;}
        attendre(0.01);
		 if(MAR.x<690 || MAR.x>870){MAR.y=324;}
		 image=MAR.img_right[i];
		 blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);

		 draw_sprite(buffer,image,MAR.x,MAR.y);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 MAR.x += 1;

		}
		
if (key[KEY_LEFT] && MAR.x > 0){if(i>4){i=0;}
         attendre(0.01);
		 if(MAR.x<690 || MAR.x>870){MAR.y=324;}
	     
		 image=MAR.img_left[i];
		 i+=1;
		 blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
		 draw_sprite(buffer,image,MAR.x,MAR.y);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 MAR.x -= 1;
 
		
		}
if(key[KEY_UP]){
		
		if(MAR.x>720 && MAR.x<870){
		
			for(i=0;i<22;i++){
			MAR.y -= i;
			blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
			draw_sprite(buffer,image,MAR.x,MAR.y);
			blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
	 
		    attendre(0.0009);
		
			}
			MAR.y=200;
			blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
			draw_sprite(buffer,image,MAR.x,MAR.y);
			blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
			}
		else{
		for(i=0;i<22;i++){
		MAR.y -= i;
		blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
 
        attendre(0.0009);
    
		}
		for(i=0;i<22;i++){
		MAR.y += i;
		blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
		draw_sprite(buffer,image,MAR.x,MAR.y);	
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);

 
        attendre(0.01);
    
		}i=0;}}
		
		
if(key[KEY_A] && MAR.y < SCREEN_H && MAR.x < SCREEN_W -50){
		if(image==MAR.img_right[0] || image==MAR.img_right[1] || image==MAR.img_right[2] || image==MAR.img_right[3] || image==MAR.img_right[4]){
			image=MAR.img_right[0];
		for(i=0;i<22;i++){			 
			j=j+1;
			MAR.y -= i;
			MAR.x += j/4;
		if(MAR.x>120 && MAR.x<250 ){ing=background[1];}
		if(MAR.x>410 && MAR.x<536 ){ing=background[2];}
		if(MAR.x>759 && MAR.x<689 ){ing=background[3];}
		if(MAR.x>1000 && MAR.x<1100){ing=background[4];}
		blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
 
        attendre(0.001);
		}
		for(i=0;i<22;i++){
		MAR.y += i;		 
		MAR.x += j/4;
		if(MAR.x>120 && MAR.x<250 ){ing=background[1];}
		if(MAR.x>410 && MAR.x<536 ){ing=background[2];}
		if(MAR.x>759 && MAR.x<689 ){ing=background[3];}
		if(MAR.x>1000 && MAR.x<1100){ing=background[4];}
		blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);

 
        attendre(0.01);
        
		}i=0;
		 j=0;}
		 
		 
		 
		 
		 
		 else{
		 image=MAR.img_left[0];
		for(i=0;i<22;i++){
		j=j+1;
		MAR.y -= i;
		MAR.x -= j/4;
		blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);

        attendre(0.01);
    
		}
		for(i=0;i<22;i++){
		MAR.y += i;
		MAR.x -= j/4;
		
		blit(ing,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
 
        attendre(0.01);
    
		}i=0;
		 j=0;}
		 
		 }

		 }
		
		
		
			 	

	
	destroy_bitmap(buffer);

	return 0;
	}
 
    
 
void attendre(float temps)
{
    clock_t arr;
    arr=clock()+(temps*CLOCKS_PER_SEC); 
 
    while(clock()<arr);
}
